package com.xt.shop.Controller;

import com.xt.shop.Model.Option;
import com.xt.shop.Model.RestBean;
import com.xt.shop.Model.UserInfo;
import com.xt.shop.service.impl.UserInfoServiceImpl;
import com.xt.shop.util.PasswordEncoderUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Resource
    UserInfoServiceImpl userInfoService;

    @Resource
    PasswordEncoderUtil passwordEncoderUtil;

    @RequestMapping("options")
    public RestBean getAllOption() {
        List<UserInfo> list = userInfoService.list();
        List<Option> options = new ArrayList<>();
        for (UserInfo userInfo : list) {
            Option option = new Option(userInfo.getUsername(), userInfo.getUserId());
            options.add(option);
        }
        return RestBean.success(options);
    }

    @RequestMapping("/select/{page}/{item}")
    public RestBean queryList(@PathVariable int page, @PathVariable int item, String name) {
        return RestBean.success(userInfoService.queryList(page, item, name));
    }

    @RequestMapping("/get/{id}")
    public RestBean get(@PathVariable String id){
        UserInfo byId = userInfoService.getById(id);
        return byId == null ? RestBean.failure(400,"参数异常"):RestBean.success(byId);
    }

    @RequestMapping("/del/{id}")
    public RestBean del(@PathVariable String id) {
        UserInfo byId = userInfoService.getById(id);
        if (byId != null) {
            byId.setState(-1);
            byId.setUpdateTime(new Date());
            if (userInfoService.updateById(byId)) {
                return RestBean.success("删除成功");
            }
            return RestBean.failure(500, "删除失败");
        }
        return RestBean.failure(400, "参数异常");
    }

    @RequestMapping("/insert")
    public RestBean insert(@RequestBody UserInfo userInfo){
        String uuid = UUID.randomUUID().toString();
        userInfo.setUserId(uuid);
        userInfo.setInputTime(new Date());
        userInfo.setUpdateTime(new Date());
        userInfo.setPassword(PasswordEncoderUtil.encoder(userInfo.getPassword()));
        if(userInfoService.save(userInfo)){
            return RestBean.success(userInfo);
        }
        return RestBean.failure(500,"保存失败");
    }

    @RequestMapping("/update")
    public RestBean update(@RequestBody UserInfo userInfo){
        String userId = userInfo.getUserId();
        UserInfo byId = userInfoService.getById(userId);
        if (byId != null){
            userInfo.setUpdateTime(new Date());
            userInfo.setInputTime(byId.getInputTime());
            if (userInfo.getPassword() == null){
                userInfo.setPassword(byId.getPassword());
            }else{
                userInfo.setPassword(PasswordEncoderUtil
                        .encoder(userInfo.getPassword()));
            }
            if (userInfoService.updateById(userInfo)) {
                return RestBean.success(userInfo);
            }
            return RestBean.failure(500,"更新失败");
        }
        return RestBean.failure(400,"参数异常");
    }


}
